package ui.prototypes;

import java.util.Calendar;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

public class CashFlow implements Parcelable {

    private long mId = -1;

    private String mDescription;

    private double mValue;

    private int mDayOfMonth;

    private int mMonth;

    private int mYear;

    public CashFlow() {
        Calendar c = Calendar.getInstance();

        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDayOfMonth = c.get(Calendar.DAY_OF_MONTH);
    }

    public CashFlow(long id) {
        this();
        mId = id;
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        mId = id;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        mDescription = description;
    }

    public double getValue() {
        return mValue;
    }

    public void setValue(double value) {
        mValue = value;
    }

    public int getDayOfMonth() {
        return mDayOfMonth;
    }

    public void setDayOfMonth(int dayOfMonth) {
        mDayOfMonth = dayOfMonth;
    }

    public int getMonth() {
        return mMonth;
    }

    public void setMonth(int month) {
        mMonth = month;
    }

    public int getYear() {
        return mYear;
    }

    public void setYear(int year) {
        mYear = year;
    }

    public Calendar getDate() {
        Calendar c = Calendar.getInstance();
        c.clear();

        c.set(Calendar.DAY_OF_MONTH, mDayOfMonth);
        c.set(Calendar.MONTH, mMonth);
        c.set(Calendar.YEAR, mYear);

        return c;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeLong(mId);
        parcel.writeString(mDescription);
        parcel.writeDouble(mValue);
        parcel.writeInt(mDayOfMonth);
        parcel.writeInt(mMonth);
        parcel.writeInt(mYear);
    }

    public static Parcelable.Creator<CashFlow> CREATOR = new Parcelable.Creator<CashFlow>() {

        @Override
        public CashFlow createFromParcel(Parcel source) {
            CashFlow cf = new CashFlow(source.readLong());

            cf.setDescription(source.readString());
            cf.setValue(source.readDouble());
            cf.setDayOfMonth(source.readInt());
            cf.setMonth(source.readInt());
            cf.setYear(source.readInt());

            return cf;
        }

        @Override
        public CashFlow[] newArray(int size) {
            return new CashFlow[size];
        }
    };

    public static CashFlow readFromCursor(Cursor c, CashFlow cf) {

        cf.setId(c.getLong(c.getColumnIndex(CashFlowContract.CashFlowTable.ID)));
        cf.setDescription(c.getString(c.getColumnIndex(CashFlowContract.CashFlowTable.DESCRIPTION)));
        cf.setValue(c.getDouble(c.getColumnIndex(CashFlowContract.CashFlowTable.VALUE)));

        long date = c.getLong(c.getColumnIndex(CashFlowContract.CashFlowTable.DATE));
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(date);

        cf.setDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        cf.setMonth(calendar.get(Calendar.MONTH));
        cf.setYear(calendar.get(Calendar.YEAR));

        return cf;
    }

    public static CashFlow read(Context ctx, long id) {
        CashFlow cf = null;

        Uri _uri = Uri.withAppendedPath(CashFlowContract.CashFlowTable.CONTENT_URI, String.format("%d", id));

        ContentResolver resolver = ctx.getContentResolver();

        Cursor c = null;

        try {
            c = resolver.query(_uri, null, null, null, null);

            if (c != null && c.moveToFirst()) {
                cf = new CashFlow();
                CashFlow.readFromCursor(c, cf);
            }
        } finally {
            if(c != null) {
                c.close();
            }
        }

        return cf;
    }

    public boolean update(Context context) {

        ContentResolver resolver = context.getContentResolver();

        Uri _uri = Uri.withAppendedPath(CashFlowContract.CashFlowTable.CONTENT_URI, String.format("%d", getId()));

        int numRows = resolver.update(_uri, toContentValues(), null, null);

        return numRows == 1;
    }

    public boolean store(Context context) {

        if(mId != -1) {
            return update(context);
        }

        ContentResolver resolver = context.getContentResolver();

        Uri _uri = resolver.insert(CashFlowContract.CashFlowTable.CONTENT_URI, toContentValues());

        return _uri != null;
    }

    public ContentValues toContentValues() {
        ContentValues cv = new ContentValues();

        cv.put(CashFlowContract.CashFlowTable.DESCRIPTION, getDescription());
        cv.put(CashFlowContract.CashFlowTable.VALUE, getValue());
        cv.put(CashFlowContract.CashFlowTable.DATE, getDate().getTimeInMillis());

        return cv;
    }
}
